package siau.android.base

import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.DisplayMetrics
import android.view.View
import android.view.WindowManager
import android.widget.FrameLayout
import android.widget.TextView
import hhm.android.base.activity.BaseActivity

open class SBBaseActivity : BaseActivity() {

    private var show: LoadingDialog? = null
    private val handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            show.let {
                it?.dismiss()
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTitleWhiteAndTextBlank()
    }

    protected fun initTitle() {
        initTitle("")
    }

    protected fun initTitle(s: String) {
        findViewById<FrameLayout>(R.id.top_back).setOnClickListener { finish() }
        findViewById<TextView>(R.id.top_title).text = s
    }

    protected fun initTitleWithLine() {
        initTitleWithLine("")
    }

    protected fun initTitleWithLine(s: String) {
        findViewById<FrameLayout>(R.id.top_back).setOnClickListener { onBackPressed() }
        findViewById<TextView>(R.id.top_title).text = s
        findViewById<View>(R.id.title_bar_line).visibility = View.VISIBLE
    }

    protected fun showLoadingDialog() {
        val loadBuilder = LoadingDialog.Builder(this).setShowMessage(false).setCancelable(false).setCancelOutside(false)
        show = loadBuilder.create()
        show?.show()
    }

    protected fun hideLoadingDialog() {
        handler.sendEmptyMessageDelayed(0, 300)
    }


    protected fun childActivityCloseLoadingDialog() {
    }


    /**
     * 获取当前屏幕的尺寸大小，不包含状态栏
     * @param context
     * @return
     */
    open fun getMetrics(context: Context): DisplayMetrics? {
        val metrics = DisplayMetrics()
        val manager: WindowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        manager.defaultDisplay.getMetrics(metrics)
        return metrics
    }

    /**
     * 获取当前屏幕包含状态栏的尺寸大小
     * @param context
     * @return
     */
    open fun getMetricsFull(context: Context): DisplayMetrics? {
        val metrics = DisplayMetrics()
        val windowMgr: WindowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        windowMgr.defaultDisplay.getRealMetrics(metrics)
        return metrics
    }

    override fun onDestroy() {
        handler.removeCallbacksAndMessages(null)
        super.onDestroy()
    }
}